package com.example.springboottools.mail;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Properties;


public class MailUtilTest {
	private static String HOST;
	private static String USERNAME;
	private static String PASSWORD;
	private static String FROMEMAIL;
    private final static String NEEDAUTH = "true";// 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
	
	protected static final Logger LOG = LoggerFactory.getLogger(MailUtilTest.class);
	private static Properties propertiesReader = new Properties();

	static {
		try {
/*			propertiesReader
					.load(new BufferedInputStream(FileUtil.class
							.getClassLoader().getResourceAsStream(
									"emailConfig.properties")));
			HOST = propertiesReader.getProperty("HOST");
			USERNAME = propertiesReader.getProperty("USERNAME");
			PASSWORD = propertiesReader.getProperty("PASSWORD");
			FROMEMAIL = propertiesReader.getProperty("FROMEMAIL");*/
			HOST = "smtp.stbchina.cn";
			USERNAME = "vpadmin";
			PASSWORD ="$tb0330#%@";
			FROMEMAIL = "vpadmin@stbchina.cn";
		} catch (Exception e) {
			LOG.error("",e);
		}
	}
	
    public static JavaMailSenderImpl getPostMan(){
    	JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
   /* 	senderImpl.setHost("smtp.staples.cn");// 设定mail server
		senderImpl.setUsername("vpsupport@staples.cn");
		senderImpl.setPassword("Staples@china_VP");*/
    /*	senderImpl.setHost("smtp.stbchina.cn");// 设定mail server
		senderImpl.setUsername("vpadmin");
		senderImpl.setPassword("$tb0330#%@");
		Properties prop = new Properties();
		prop.put("mail.smtp.auth","true");
		prop.put("mail.smtp.starttls.enable", "true");
		
		prop.put("mail.smtp.connectiontimeout","25000");
		prop.put("mail.smtp.timeout","25000");
		senderImpl.setJavaMailProperties(prop);*/
        senderImpl.setHost("smtp.stbchina.cn");// 设定mail server
        senderImpl.setUsername("vpadmin");
        senderImpl.setPassword("$tb0330#%@");
        senderImpl.setPort(25);
    	return senderImpl;
    }
    /**
     * 
     * <b>方法说明：</b>
     * 发送简单邮件
     * @param fromEmail
     * @param toEmail
     * @param subject
     * @param text
     * @return
     */
	public static boolean SingleMailSend(String fromEmail, String[] toEmail,String subject,String text) {
		JavaMailSenderImpl senderImpl = getPostMan();		
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setTo(toEmail);
		mailMessage.setFrom(fromEmail);
		mailMessage.setSubject(subject);
		mailMessage.setText(text);
		// 发送邮件
		senderImpl.send(mailMessage);
		return true;
	}
	/**
	 * 
	 * <b>方法说明：</b>
	 * 发送html类容的邮件
	 * @param fromEmail
	 * @param toEmail
	 * @param subject
	 * @param text
	 * @return
	 */
	public static boolean sendHtmlMail(String[] toEmail,String subject,String text) {
		JavaMailSenderImpl senderImpl = getPostMan();
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		MimeMessageHelper messageHelper;
		// 设置收件人，寄件人
		try {
			messageHelper = new MimeMessageHelper(mailMessage,true,"GBK");
			messageHelper.setTo(toEmail);	
			messageHelper.setFrom("vpadmin@stbchina.cn");
			messageHelper.setSubject(subject);
			messageHelper.setText(text,true);
			// 发送邮件 fixed issue of sending exception 
			senderImpl.send(mailMessage);
		} catch (MessagingException e) {
			LOG.error("设置邮件参数发生错误！", e);
			return false;
		} catch (Exception e) {
			LOG.error("发送邮件时候出错!", e);
			return false;
		}
		return true;
	}
	/**
	 * 
	 * <b>方法说明：</b>
	 * 发送HTML邮件中带图片
	 * @param fromEmail
	 * @param toEmail
	 * @param subject
	 * @param text
	 * @param imgPath
	 * @param imgName
	 * @return
	 */
	public static boolean AttachedImageMail(String fromEmail, String[] toEmail,String subject,String text,String imgPath,String imgName){
		JavaMailSenderImpl senderImpl = getPostMan();
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		// 注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，
		// multipart模式
		MimeMessageHelper messageHelper = null;
		try {
			messageHelper = new MimeMessageHelper(mailMessage,true);
		} catch (MessagingException e) {
			LOG.error("邮件发送失败！", e);
			return false;
		}
		// 设置收件人，寄件人
		try {
			messageHelper.setTo(toEmail);
			messageHelper.setFrom(fromEmail);
			messageHelper.setSubject(subject);
			// true 表示启动HTML格式的邮件
			messageHelper.setText(text, true);
		} catch (MessagingException e) {
			LOG.error("设置邮件参数发生错误！", e);
			return false;
		}
		FileSystemResource img = new FileSystemResource(new File(imgPath));
		try {
			messageHelper.addInline(imgName, img);
		} catch (MessagingException e) {
			LOG.error("嵌入图片错误！", e);
		}
		// 发送邮件
		senderImpl.send(mailMessage);
		return true;
	}
	/**
	 * 
	 * <b>方法说明：</b>
	 * 发送带附件的邮件
	 * @param fromEmail
	 * @param toEmail
	 * @param subject
	 * @param text
	 * @param filePath
	 * @param fileName
	 * @return
	 */
	public static boolean AttachedFileMail(String fromEmail, String[] toEmail,String subject,String text,String filePath,String fileName){
		JavaMailSenderImpl senderImpl = getPostMan();
		// 建立邮件消息,发送简单邮件和html邮件的区别
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		// 注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，
		// multipart模式 为true时发送附件 可以设置html格式
		MimeMessageHelper messageHelper = null;
		try {
			messageHelper = new MimeMessageHelper(mailMessage,true, "utf-8");
		} catch (MessagingException e) {
			LOG.error("邮件发送失败！", e);
			return false;
		}
		// 设置收件人，寄件人
		try {
			messageHelper.setTo(toEmail);
			messageHelper.setFrom(fromEmail);
			messageHelper.setSubject(subject);
			// true 表示启动HTML格式的邮件
			messageHelper.setText(text,true);
		} catch (MessagingException e) {
			LOG.error("设置邮件参数发生错误！", e);
			LOG.error(e.getLocalizedMessage());
			return false;
		}
		FileSystemResource file = new FileSystemResource(new File(filePath));
		// 这里的方法调用和插入图片是不同的。
		try {
			messageHelper.addAttachment(fileName, file);
		} catch (MessagingException e) {
			LOG.error("添加附件错误！", e);
		}
		// 发送邮件
		senderImpl.send(mailMessage);
		return true;
	}

	public static void main(String[] args) {
		String[] toEmail= {"bochuanglianhang@163.com"};
		String subject="staples供应商注册邮箱检验";
		String text="<html>尊敬的用户：<br/><div style='margin-left:30px;'><br/> 四川博创联航科技有限公司,您好！<br/>您在2021-03-30注册史泰博供应商管理系统，请点击下面的链接激活用户<br/><br/><a href='http://newvp.staplescn.com//VendorPortal/system/VPRegistionCtr/emailValidate?id=Mjg3MTE2MQ==&lostTimeToDb=MjAyMS0wMy0zMA=='>点击完成激活</a><br/><br/> (如果您无法点击这个链接，请将此链接复制到浏览器地址栏后访问)<br/> 为了保证您帐号的安全性，该链接有效期为3天，并且点击一次后将失效!<br/>邮箱激活后请即时登录系统并完成企业信息录入。<br/> 如果您误收到此电子邮件，则可能是其他用户在尝试帐号设置时的误操作，如果您并未发起该请求，则无需再进行任何操作，并可以放心地忽略此电子邮件。<br/><br/>请勿直接回复本邮件。<br/><br/>感谢您使用史泰博供应商管理系统<a href=''></a>！<div style='text-align:center;margin:0 auto;'><br/>史泰博<br/>2021-03-30</div><br/>此邮件为自动发送，请勿回复！<br/></div></html>";
		try {
			MailUtilTest.sendHtmlMail(toEmail,subject,text);
		}catch (Exception e){
			e.getMessage();
		}

	}
}
